package com.wing.cms.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wing.cms.domain.Artgroup;
import com.wing.cms.domain.Channel;
import com.wing.cms.service.ArtgroupManager;
import com.wing.cms.service.ArticleManager;
import com.wing.cms.service.Article_groupManager;
import com.wing.common.Constants;
import com.wing.common.util.PageBean;

/**
 * 
 * 
 * @author please  write your big name
 * 
 */
 
@Controller
@RequestMapping(value = "/cms/artgroup")
public class ArtgroupController {

	@Autowired
	private ArtgroupManager artgroupManager;
	@Autowired
	private Article_groupManager article_groupManager;
	@Autowired
	private ArticleManager articleManager;
	
	
	/**
	 * 获得列表数据
	 *  
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "/cms/artgroup/artgroupIndex";
	}
	
	@RequestMapping(value = "get/{url}", method = RequestMethod.GET)
	public String delete(@PathVariable String url) {
		return "/cms/artgroup/"+url;
	}
		
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public @ResponseBody PageBean list(int currentPage, String pageSize,
			String pid,HttpServletRequest request) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  artgroupManager.findPageList(currentPage, _iPageSize,pid,siteId);
	}
	
	/**
	 * 打开编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toForm/{id}", method = RequestMethod.GET)
	public String toEditPage(@PathVariable String id,String pid,
			Model model,HttpServletRequest request) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		Artgroup artgroup = null;
		if(!id.equals("0")){
			artgroup = artgroupManager.findById(id);
		}else{
			artgroup = new Artgroup();
			artgroup.setPid(pid);
			artgroup.setSite_id(siteId);
		}
		model.addAttribute("artgroup", artgroup);
		return "/cms/artgroup/artgroupForm";
	}
	
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public  @ResponseBody  String save(Artgroup artgroup) {
		artgroupManager.saveArtgroup(artgroup);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
	
	@RequestMapping(value = "delartgroup/{id}", method = RequestMethod.POST)
	public @ResponseBody String delartgroups(@PathVariable String id,HttpServletRequest request) throws IOException {
		if(Artgroup.ARTGROUPROOT.equals(id)){
			return "0";
		}
		artgroupManager.deleteArtgroups(id,request);
		return "1";
	}
	
	@RequestMapping(value = "grpArtlst", method = RequestMethod.POST)
	public @ResponseBody PageBean grpArtlst(int currentPage, String pageSize,
			String grpId,String seachv,HttpServletRequest request) {
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  articleManager.findByGroupId(currentPage, _iPageSize,grpId,seachv);
	}
		
//	@RequestMapping(value = "artlst", method = RequestMethod.POST)
//	public @ResponseBody PageBean artlst(int currentPage, String pageSize,
//			HttpServletRequest request) {
//		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
//		int _iPageSize = 0;
//		if (pageSize == null) {
//			_iPageSize = PageBean.DEFAULT_PAGESIZE;
//		} else {
//			_iPageSize= Integer.parseInt(pageSize);
//		}
//		return  articleManager.findPageList(currentPage, _iPageSize,siteId);
//	}
	
	@RequestMapping(value = "addArt", method = RequestMethod.POST)
	public  @ResponseBody  String addArt(String grpId,String artIds,HttpServletRequest request) {
		artgroupManager.addArtForGrp(grpId,artIds);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
	
	/**
	 * 移除频道下的文章
	 * @param channelId
	 * @param artIds
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "remArt", method = RequestMethod.POST)
	public  @ResponseBody  String remArt(String grpId,String[] chk,HttpServletRequest request) {
		article_groupManager.deleteArticle_groupsByGrpIdArtId(grpId,chk);
		
		return ""+chk.length;
	}
	
	/**
	 * 是根据文章组parentId获取的文章组,生成树
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 *  
	 * @throws ServletException, IOException
	 */
	@RequestMapping(value = "showTree", method = RequestMethod.GET)
	public void showTree(HttpServletRequest request,
			HttpServletResponse response) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Pragma", "No-cache");//HTTP 1.1    
		response.setHeader("Cache-Control", "no-cache");//HTTP 1.0    
		response.setHeader("Expires", "0");//防止被proxy
		
		String pid = request.getParameter("id");
		boolean firstFlag = false;
		if (pid == null || pid.equalsIgnoreCase("")) {
			pid = Artgroup.ARTGROUPROOT;//我数据库中一级节点的parentid都是-1
			firstFlag = true;
		}
		try {
			//根据上一级节点,查询该节点下的所有节点 
//			String aa = codeManager.createDHTMLXML(pid,firstFlag,domainId);
			response.getWriter()
					.write(artgroupManager.createDHTMLXML(pid,firstFlag,siteId));
			response.getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

